package net.lovesrc.allin.jobs.backup;

import net.lovesrc.allin.biz.lbc.BaseLbcService;
import net.lovesrc.allin.biz.lbc.LbcService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;

import java.util.concurrent.FutureTask;

//@Component
@Profile("geliclive")
public class LbcProfitJob extends LbcBaseJob {

    @Autowired
    private LbcService lbcService;

    @Override
    public void runOnce() throws Exception {
        if (RandomUtils.nextInt(0, 100) == 10 && lbcService.isNotJyTime()) {
            jdbcTemplate.update("update t_lbc_account set nextProfitTime = date(now()) + interval (8 + rand() * 2.5) * 60 * 60 second " +
                    "where nextProfitTime < date(now()) and worked = 1 and datediff(now(), profitTime) > 1 ");

            jdbcTemplate.update("update t_lbc_account set profitTime = date(now()) - interval 24 * 60 * 60 second " +
                    "where worked = 1 and mobile in (select mobile from (select * from t_lbc_account where profitResText like '%html%') a)");
        }
        FutureTask<Object> futureTask1 = new FutureTask(() -> {
            boolean proxy = tryGetProperties(boolean.class, true, "LBC_PROFIT_JOB_PROXY_59");
            if (proxy) {
                lbcService.profits(1, BaseLbcService.Proxy.PROXY_GELIC, true);
            }
        }, null);
        FutureTask<Object> futureTask2 = new FutureTask(() -> {
            lbcService.profits(1, BaseLbcService.Proxy.PROXY_GELIC);
        }, null);

        execute(futureTask1);
        execute(futureTask2);

        futureTask1.get();
        futureTask2.get();
    }

    @Override
    protected long intervalMilliseconds() {
        return 1000 * RandomUtils.nextInt(3, 6);
    }

}
